package com.lgq.cfw.controller.permission;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lgq.cfw.bean.TableRole;
import com.lgq.cfw.bean.message.Message;
import com.lgq.cfw.service.RoleService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Lin
 * @date 2019-02-08 23:29
 **/
@RestController
@RequestMapping(value = "/permission/role")
public class RoleController {
  @Autowired
  RoleService roleService;

  /**
   * 获取用户所有角色信息
   * @param userId 用户id
   * @return 该用户的角色信息
   */
  @RequestMapping(value = "/getRole")
  public Message getRole(@RequestParam(value = "userId")Integer userId){
    //获取已分配的角色列表
    List<TableRole> assignedRoleList=roleService.getRoleById(userId);
    //将用户有的角色放入map中
    Map<Integer,TableRole> assignedRoleMap=new HashMap<>(30);
    for (TableRole assignedRole : assignedRoleList) {
      assignedRoleMap.put(assignedRole.getId(),assignedRole);
    }
    //获取全部的角色列表
    List<TableRole> totalRoleList=roleService.getAllRole();
    List<TableRole> unAssignedRoleList=new ArrayList<>();
    //获取用户未拥有的角色
    for (TableRole tableRole : totalRoleList) {
      if (!(assignedRoleMap.containsKey(tableRole.getId()))){
        unAssignedRoleList.add(tableRole);
      }
    }
    return Message.success("处理成功",null)
        .add("assignedRoleList",assignedRoleList).add("unAssignedRoleList",unAssignedRoleList);
  }

  /**
   * 获取所有角色信息
   * @param currentPage 当前页面
   * @return 角色信息
   */
  @RequestMapping(value = "/getAll",method = RequestMethod.GET)
  public Message getAllRole(@RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage){
    //设置每页数据量
    PageHelper.startPage(currentPage,10);
    //获取所有角色信息
    List<TableRole> tableRoleList=roleService.getAllRole();
    //传入数据到PageInfo中
    PageInfo pageInfo=new PageInfo(tableRoleList,5);
    return Message.success("处理成功",null).add("pageInfo",pageInfo);
  }

  /**
   * 批量和单个删除角色
   * @param roleDeleteIdString 要删除的角色的id字符串
   * @return 处理信息
   */
  @RequestMapping(value = "/delete/{roleDeleteIdString}",method = RequestMethod.DELETE)
  public Message delete(@PathVariable(value = "roleDeleteIdString") String roleDeleteIdString){
    boolean flag=roleService.delete(roleDeleteIdString);
    if (flag) {
      return Message.success("处理成功", null);
    }else {
      return Message.fail("处理失败",null);
    }
  }

  /**
   * 添加角色信息
   * @param roleName 角色名
   * @return 处理信息
   */
  @RequestMapping(value = "/add",method = RequestMethod.POST)
  public Message addRole(@RequestParam(value = "roleName") String roleName){
    roleService.addRole(roleName);
    return Message.success("处理成功",null);
  }

  /**
   * 更新角色信息
   * @param tableRole 传入的角色
   * @return 处理信息
   */
  @RequestMapping(value = "/update",method = RequestMethod.POST)
  public Message updateRoleName(TableRole tableRole){
    roleService.updateRoleName(tableRole);
    return Message.success("处理成功",null);
  }


}
